package com.wyp.didishige.activity;

import android.annotation.SuppressLint;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

import com.bmob.pay.tool.BmobPay;
import com.umeng.analytics.MobclickAgent;
import com.wyp.didishige.config.Config;
import com.wyp.didishige.utils.IntentUtil;
import com.wyp.didishige.R;
import com.wyp.didishige.model.User;


import cn.bmob.im.BmobChat;

/**
 * 引导页
 *
 * @author smile
 * @ClassName: SplashActivity
 * @Description: TODO
 * @date 2014-6-4 上午9:45:43
 */
public class SplashActivity extends BaseActivity {

    private static final int GO_HOME = 100;
    private static final int GO_LOGIN = 200;
    private TextView mVersion;
    private View mTopLay;
    Animation mTopIn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE); //设置程序不显示标题
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN); //设置不显示状态栏
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_splash);
        mTopIn = AnimationUtils.loadAnimation(this, R.anim.hr_dialog_bottom_enter_anim);
        //可设置调试模式，当为true的时候，会在logcat的BmobChat下输出一些日志，包括推送服务是否正常运行，如果服务端返回错误，也会一并打印出来。方便开发者调试
        BmobChat.DEBUG_MODE = true;
        //BmobIM SDK初始化--只需要这一段代码即可完成初始化
        //请到Bmob官网(http://www.bmob.cn/)申请ApplicationId,具体地址:http://docs.bmob.cn/android/faststart/index.html?menukey=fast_start&key=start_android
        BmobChat.getInstance(this).init(Config.applicationId);
        BmobPay.init(this, Config.applicationId);
        MobclickAgent.updateOnlineConfig(this);
        init();
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        mTopLay = findViewById(R.id.mTopLay);
        mVersion= (TextView) findViewById(R.id.mVersion);

        PackageManager packageManager=getPackageManager();

        try {
            PackageInfo packageInfo=packageManager.getPackageInfo(this.getPackageName(),0);
            mVersion.setText(packageInfo.versionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        mTopLay.startAnimation(mTopIn);
    }
    @Override
    protected void initWidgetListener() {
        super.initWidgetListener();
        mTopIn.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                mTopLay.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

    }
    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
//        mHandler.sendEmptyMessageDelayed(GO_HOME, 2000);
        if (userManager.getCurrentUser() != null) {
            // 每次自动登陆的时候就需要更新下当前位置和好友的资料，因为好友的头像，昵称啥的是经常变动的
            updateUserInfos();
            mHandler.sendEmptyMessageDelayed(GO_HOME, 3000);
        } else {
            mHandler.sendEmptyMessageDelayed(GO_LOGIN, 3000);
        }
    }


    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case GO_HOME:
                    IntentUtil.start_activity(SplashActivity.this, MainActivity.class);
                    finish();
                    break;
                case GO_LOGIN:
                    IntentUtil.start_activity(SplashActivity.this, LoginActivity.class);
                    finish();
                    break;
            }
        }
    };

}
